<?php

namespace Admin\Controller;

class IndexController extends AdminController {

    public function index() {
        $listMenu = $this->listMenu();
        foreach($listMenu as $key=>$val){
            if($val['href'] == 'article/index'){
                $listMenu[$key]['child'] = array_merge($val['child'],$this->rendArticleCategory());
            }
        }
        $menuHtml = '';
        foreach ($listMenu as $menu) {
            $menuHtml .= $this->rendMenu($menu);
        }
        $this->assign('menuHtml', $menuHtml);
        $this->assign('adminInfo', $this->admin_info);
        $this->display();
    }

    public function dashboard() {
        $this->display();
    }

    public function password() {
        if (IS_POST) {
            $old_password = I('old_password');
            $password = I('password');
            $re_password = I('re_password');
            $len = strlen($password);
            if ($len < 6 || $len > 16) {
                $this->error('密码长度必须为6-16位');
            }
            if ($password != $re_password) {
                $this->error('两次输入密码不相同');
            }
            $old_password = entrypt_password($old_password);
            if ($old_password != $this->admin_info['password']) {
                $this->error('原始密码不正确');
            }           
            $password = entrypt_password($password);
             if($old_password == $password){ 
                $this->error('新密码不能和原始密码相同');
            }
            if (M('Manager')->where(['id' => $this->admin_info['id']])->setField('password', $password) !== false) {
                $this->admin_info['password'] = $password;
                $auth = $this->admin_info['id']."\t".$password;
                cookie('_auth',  authcode($auth,'ENCODE'));
                $this->success('密码修改成功','',false);
            }else{
                $this->error('密码修改失败');
            }
        } else {
            $this->display();
        }
    }

    public function listMenu($hide = false) {
        static $menuList = [];
        if (empty($menuList)) {
            $where = $hide ? [] : ['status' => 1];
            $menuList = M('AdminMenu')->where($where)->order('sort ASC,id ASC')->select();
            foreach ($menuList as $key => $val) {
                $menuList[$key]['url'] = U($val['url']);                
            }
            if (!$this->admin_info['supper']) {
                $mids = [];
                foreach ($this->purviews as $purview) {
                    $mids[] = $purview['mid'];
                }

                $mids = array_unique($mids);
                $menus = [];
                if (!$mids) {
                    $mids = [0];
                }
                $modules = M('PurviewModule')->where(['id' => ['in', $mids]])->getField('menus', true); //查找拥有的权限模块菜单                
                foreach ($modules as $module) {
                    $menus = array_merge($menus, string2array($module));
                }
                $menus = $this->getParentMenu(array_unique($menus)); //全部菜单ID

                foreach ($menuList as $key => $menu) {
                    if (!in_array($menu['id'], $menus) && $menu['auth'] == 1) {
                        unset($menuList[$key]);
                    }                   
                }
            }
            $menuList = tree_array($menuList);
        }
        return $menuList;
    }
    
    public function rendArticleCategory(){ 
        $reuslt = M('ArticleCategory')->order('pid ASC,sort ASC,id ASC')->select();
        foreach ($reuslt as $key=>$val){
            $reuslt[$key]['url'] = U('article/index',['category_id'=>$val['id']]);
            if($val['pid'] != 0){ 
                 $reuslt[$key]['icon'] = 'glyphicon glyphicon-tag';
            }
        }
        $reuslt = tree_array($reuslt);
        foreach($reuslt as $key=>$val){ 
            if(!empty($val['child'])){
                $reuslt[$key]['icon'] = 'glyphicon glyphicon-folder-open';
            }else{
                $reuslt[$key]['icon'] = 'glyphicon glyphicon-tag';
            }
        }
        return $reuslt;
    }

    public function getParentMenu($ids) {

        if (!$ids) {
            $ids = [0];
        }
        $menus = M('AdminMenu')->where(['id' => ['in', $ids]])->select();
        foreach ($menus as $menu) {
            if ($menu['pid'] != 0 && !in_array($menu['pid'], [$ids])) {
                $ids = array_merge($ids, $this->getParentMenu([$menu['pid']]));
            }
        }
        return $ids;
    }

    public function rendMenu($menu) {
        $menu_icon = $menu['icon'] ? '<i class="menu-icon ' . $menu['icon'] . '"></i>' : '';
        if($menu['href']){ 
            $menu['url'] = U($menu['href']);
        }
        
        if (empty($menu['child'])) {
            $str = <<<str
          <li>
                <a href="{$menu['url']}" target="rightMain">
                    {$menu_icon}
                    <span class="menu-text"> {$menu['name']} </span>
                </a>
            </li>
str;
        } else {
            $child_str = '';
            foreach ($menu['child'] as $cmenu) {

                $child_str .= $this->rendMenu($cmenu);
            }
            $str = <<<str
        <li>
                        <a href="{$menu['url']}" target="rightMain" class="menu-dropdown">
                            {$menu_icon}
                            <span class="menu-text"> {$menu['name']} </span>
                            <i class="menu-expand"></i>
                        </a>
                        <ul class="submenu">                        	
                           {$child_str} 
                        </ul>
                    </li> 
str;
        }
        return $str;
    }

}
